<?php

class ContactController extends Controller {

    public function actionIndex() {
        $this->pageTitle = Yii::t('app', 'Manager contact person');

        $model = new ContactPerson();
        $criteriaData = new CDbCriteria();
        $criteriaData->addCondition('del_flg = 0');

        $dataProvider = new CActiveDataProvider('ContactPerson', array(
            'pagination'=>array(
                'pageSize'=>SystemConfig::PAGE_SIZE,
            ),
            //fillter order by created and status
            'criteria'=> $criteriaData,
        ));

        $this->render('index', array(
            'model' => $model,
            'dataProvider'=>$dataProvider,
        ));
    }

    public function actionAdd() {
        $this->pageTitle = Yii::t('app', 'Manager contact person');

        $model = new ContactPerson();

        if (isset($_POST['ContactPerson']) && $data = $_POST['ContactPerson']) {
            $model->attributes = $data;
            if ($model->save()) {
                Yii::app()->user->setFlash('success', Yii::t('app', 'Contact created success!'));
                $this->redirect(array('contact/index'));
            }
        }

        $this->render('add', array(
            'listClient' => Client::getListClient(),
            'model' => $model
        ));
    }


    public function actionUpdate($id = null) {
        $this->pageTitle = Yii::t('app', 'Manager contact person');
        if (!$id) {
            Yii::app()->user->setFlash('error', Yii::t('app', 'Missing parameters!'));
            $this->redirect(Yii::app()->request->urlReferrer);
        }

        //Get store type
        $model = ContactPerson::model()->findByPk($id, 'del_flg = 0');

        if (!$model) {
            Yii::app()->user->setFlash('error', Yii::t('app', 'Invalid record!'));
            $this->redirect(Yii::app()->request->urlReferrer);
        }

        if (isset($_POST['ContactPerson']) && $data = $_POST['ContactPerson']) {
            $model->attributes = $data;
            if ($model->save()) {
                Yii::app()->user->setFlash('success', Yii::t('app', 'Contact updated success!'));
                $this->redirect(array('contact/index'));
            }
        }
        // get list client to select
        $mClient = Client::model()->findAll('del_flg = 0');
        $listClient = array();
        if ($mClient) {
            foreach ($mClient as $mc) {
                $listClient[$mc->id_client] = $mc->company_name;
            }
        }

        $this->render('update', array(
            'listClient' => $listClient,
            'model' => $model,
        ));
    }

    public function actionDelete($id = null) {
        if (!$id) {
            Yii::app()->user->setFlash('error', Yii::t('app', 'Missing parameters!'));
            $this->redirect(Yii::app()->request->urlReferrer);
        }

        $model = ContactPerson::model()->findByPk($id, 'del_flg = 0');

        if (!$model) {
            Yii::app()->user->setFlash('error', Yii::t('app', 'Invalid record!'));
            $this->redirect(Yii::app()->request->urlReferrer);
        }

        $model->del_flg = 1;
        if ($model->save(false)) {
            if(!isset($_GET['ajax'])){
                Yii::app()->user->setFlash('success', Yii::t('app', 'Contact - Deleted Successfully'));
                $this->redirect(array('contact/index'));
            }
            else{
                echo '<div class="alert alert-success fade in"><button type="button" class="close" data-dismiss="alert">×</button>'.Yii::t('app', 'Contact - Deleted Successfully').'</div>';
                die();
            }

        }

    }
}